<?php

namespace app\system\controller;

use app\system\model\SysConfigModel;
use Exception;
use message;
use think\Request;

class SystemConfig extends MainFrame
{
    public function showConfig(SysConfigModel $sysConfigModel)
    {
        $systemName = $sysConfigModel->getConfigValue("systemName");
        $banqInfo = $sysConfigModel->getConfigValue("banqInfo");
        $coder = $sysConfigModel->getConfigValue("coder");
        $footerInfo = $sysConfigModel->getConfigValue("footerInfo");
        $sys_config = [
            "systemName" => $systemName,
            "banqInfo" => $banqInfo,
            "coder" => $coder,
            "footerInfo" => $footerInfo
        ];
        $this->assign("sys_config", $sys_config);
        return $this->fetch();
    }

    public function saveConfig(Request $request, SysConfigModel $sysConfigModel)
    {
        try {
            $sys_config = [
                ["config_name" => "systemName", "config_value" => $request->param("systemName")],
                ["config_name" => "banqInfo", "config_value" => $request->param("banqInfo")],
                ["config_name" => "coder", "config_value" => $request->param("coder")],
                ["config_name" => "footerInfo", "config_value" => $request->param("footerInfo")],
            ];

            $obj = $sysConfigModel->setConfigValue($sys_config);
            if ($obj != null) {
                return json(["code" => 0, "msg" => "保存成功"]);
            }
            die;
        } catch (Exception $e) {
            return json(["code" => 1, "msg" => $e->getMessage()]);
        }
    }
}
